package com.charles.seckillGateway.handler;

import com.charles.seckillGateway.service.CaptchaService;
import com.charles.seckillComponentCommon.domain.AjaxResult;
import com.charles.seckillComponentCommon.exceptions.CaptchaException;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;

/**
 * @author Charles-H
 * 
 * 验证码的获取
 */
@Component
public class ValidateCodeHandler implements HandlerFunction<ServerResponse> {

    @Resource
    private CaptchaService captchaService;

    @Override
    public Mono<ServerResponse> handle(ServerRequest request) {
        AjaxResult result;
        try {
            result = captchaService.createCaptcha();
        } catch (CaptchaException e) {
            return Mono.error(e);
        }
        return ServerResponse.status(200).body(BodyInserters.fromValue(result));
    }
}
